import ActivityModel from "../models/ActivityModel";
import {observable, action, toJS} from "mobx";

class ActivitiesStore {
    api;
    appStore;
    @observable activityType = -1;
    @observable activityModel = new ActivityModel();
    @observable activities = [];

    constructor(api, appStore) {
        this.api = api;
        this.appStore = appStore;
        this.setType(-1);
    }

    @action setModel(activityModel) {
        this.activityModel = activityModel;
    }

    @action getModel() {
        return this.activityModel;
    }

    @action setActivityType(type) {
        this.activityType = type;
    }

    @action getActivityType() {
        return this.activityType;
    }

    @action setType(type) {
        this.activityModel = new ActivityModel();
        this.activityModel.type = type;
    }

    @action getType() {
        return this.activityModel.type;
    }

    //发起活动
    @action launchActivity(activityModel) {
        this.appStore.increaseRequest();
        return this.api.launchActivity(activityModel).then(
            action(data => {
                console.log("--------");
                console.log(data);
                console.log("--------");
                this.appStore.decreaseRequest();
                if (data===true) {
                    alert("发起活动成功");
                    return Promise.resolve();
                } else {
                    this.appStore.setError(data.error);
                    alert("发起活动失败");
                    return Promise.reject();
                }
            })
        );
    }

    //从服务器获取值年活动列表
    @action fetchYearActivityList() {
        this.appStore.increaseRequest();
        return this.api.getYearActivityList().then(
            action(data => {
                console.log("--------");
                console.log(data);
                console.log("--------");
                this.appStore.decreaseRequest();
                if (data) {
                    this.activities.clear();
                    data.forEach(activity => {
                        if (activity.status === 1) {
                            this.activities.push(ActivityModel.fromJS(this, activity))
                        }
                    });
                    return Promise.resolve();
                } else {
                    this.appStore.setError(data.error);
                    return Promise.reject();
                }
            })
        );
    }

    //从服务器获取普通活动列表
    @action fetchNorActivityList() {
        this.appStore.increaseRequest();
        return this.api.getNorActivityList().then(
            action(data => {
                console.log("--------");
                console.log(data);
                console.log("--------");
                this.appStore.decreaseRequest();
                if (data) {
                    this.activities.clear();
                    data.forEach(activity => {
                        if (activity.status === 1) {
                            this.activities.push(ActivityModel.fromJS(this, activity))
                        }
                    });
                    return Promise.resolve();
                } else {
                    this.appStore.setError(data.error);
                    return Promise.reject();
                }
            })
        );
    }

    //根据id获取活动详情页
    @action fetchActivityById(id) {
        this.appStore.increaseRequest();
        return this.api.getActivityByid(id).then(
            action(data => {
                console.log("--------");
                console.log(data);
                console.log("--------");
                this.appStore.decreaseRequest();
                if (data) {
                    const activity = this.getActivity(id);
                    if (activity) {
                        activity.updateFromJS(data);
                    } else {
                        this.activities.push(ActivityModel.fromJS(this, data));
                    }
                    return Promise.resolve();
                } else {
                    this.appStore.setError(data.error);
                    return Promise.reject();
                }
            })
        );
    }

    //根据活动id获取当前store中的活动信息
    getActivity(id) {
        if (this.activities.find(item => item.id == id)) {
            return this.activities.find(item => item.id == id);
        } else {
            return this.activities[0];
        }
    }
}

export default ActivitiesStore;